//
//  IFLabel2.m
//  chanlin
//
//  Created by Ryan on 14-12-16.
//  Copyright (c) 2014年 chanlin. All rights reserved.
//

#import "IFLabel2.h"
#import <CoreText/CoreText.h>

@implementation IFLabel2

- (id)init
{
    self = [super init];
    if (self) {
        self.textAlignment=kCTTextAlignmentLeft;
        self.backgroundColor=[UIColor clearColor];
        
    }
    return self;
}

- (void)buildAttributStr{
    if(_isStrNULL(self.text)){
        self.attributStr=nil;
        return;
    }
    
    
    NSMutableAttributedString *mbstring=[[NSMutableAttributedString alloc] initWithString:self.text];
    int length=[self.text length];
    //NSLog(@"%f--%f",self.font.pointSize);
    // 字体
    CTFontRef font=CTFontCreateWithName((__bridge CFStringRef)self.font.fontName, self.font.pointSize, NULL);
    [mbstring addAttribute:(id)kCTFontAttributeName value:(__bridge id)font range:NSMakeRange(0, length)];
    CFRelease(font);
    
    // 字体颜色
    if(!self.textColor){
        self.textColor=[UIColor blackColor];
    }
    
    CGColorRef color=self.textColor.CGColor;
    [mbstring addAttribute:(id)kCTForegroundColorAttributeName value:(__bridge id)color range:NSMakeRange(0, length)];
    
    // 行间距
    float lightSpacing=self.lineSpacing;
    if(lightSpacing==0){
        lightSpacing=self.font.lineHeight-self.font.pointSize;
    }
    
    // 段落对其方式
    CTTextAlignment textAlignment = self.textAlignment;
    
    CTParagraphStyleRef pStyle=NULL;
    
    if(lightSpacing!=0){
        CTParagraphStyleSetting paraStyles[3] = {
            
            {.spec = kCTParagraphStyleSpecifierMinimumLineSpacing,.valueSize = sizeof(float), .value = &lightSpacing},
            
            {.spec = kCTParagraphStyleSpecifierMaximumLineSpacing,.valueSize = sizeof(float), .value = &lightSpacing},
            
            {.spec = kCTParagraphStyleSpecifierAlignment,.valueSize = sizeof(CTTextAlignment), .value = &textAlignment}
        };
        
        pStyle=CTParagraphStyleCreate(paraStyles, 3);
        
    }else{
        CTParagraphStyleSetting paraStyles[1] = {
            {.spec = kCTParagraphStyleSpecifierAlignment,.valueSize = sizeof(CTTextAlignment), .value = &textAlignment}
        };
        
        pStyle=CTParagraphStyleCreate(paraStyles, 3);
    }
    
    [mbstring addAttribute:(id)kCTParagraphStyleAttributeName value:(__bridge id)pStyle range:NSMakeRange(0, length)];
    
    CFRelease(pStyle);
    self.attributStr=mbstring;
}

+ (float)suggestHeightOfString:(NSString *)str withWidth:(float)width font:(UIFont *)font lineSpace:(float)lineSpace{
    if(_isStrNULL(str))return 0.f;
    
    NSMutableAttributedString *mbstring=[[NSMutableAttributedString alloc] initWithString:str];
    int length=[str length];
    //NSLog(@"%f--%f",self.font.pointSize);
    // 字体
    CTFontRef reffont=CTFontCreateWithName((__bridge CFStringRef)font.fontName, font.pointSize, NULL);
    [mbstring addAttribute:(id)kCTFontAttributeName value:(__bridge id)reffont range:NSMakeRange(0, length)];
    CFRelease(reffont);
    
    float lightSpacing=lineSpace;
    if(lightSpacing==0){
        lightSpacing=font.lineHeight-font.pointSize+1;
    }
    
    // 段落对其方式
    CTTextAlignment textAlignment = kCTTextAlignmentLeft;
    
    CTParagraphStyleRef pStyle=NULL;
    
    if(lightSpacing!=0){
        CTParagraphStyleSetting paraStyles[3] = {
            
            {.spec = kCTParagraphStyleSpecifierMinimumLineSpacing,.valueSize = sizeof(float), .value = &lightSpacing},
            
            {.spec = kCTParagraphStyleSpecifierMaximumLineSpacing,.valueSize = sizeof(float), .value = &lightSpacing},
            
            {.spec = kCTParagraphStyleSpecifierAlignment,.valueSize = sizeof(CTTextAlignment), .value = &textAlignment}
        };
        
        pStyle=CTParagraphStyleCreate(paraStyles, 3);
        
    }else{
        CTParagraphStyleSetting paraStyles[1] = {
            {.spec = kCTParagraphStyleSpecifierAlignment,.valueSize = sizeof(CTTextAlignment), .value = &textAlignment}
        };
        
        pStyle=CTParagraphStyleCreate(paraStyles, 3);
    }
    
    [mbstring addAttribute:(id)kCTParagraphStyleAttributeName value:(__bridge id)pStyle range:NSMakeRange(0, length)];
    
    CFRelease(pStyle);
    
    CTFramesetterRef framesetter=CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)mbstring);
    
    CGSize size=CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, length), NULL, CGSizeMake(width, CGFLOAT_MAX), NULL);
    
    CFRelease(framesetter);
    
    return ceilf(size.height);
    
}

- (void)drawRect:(CGRect)rect{
    
    if(!self.attributStr){
     [self buildAttributStr];
    }
    if(self.attributStr){
        CTFramesetterRef framesetter=CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)self.attributStr);
        CGMutablePathRef path=CGPathCreateMutable();
        CGPathAddRect(path, NULL, self.bounds);
        CTFrameRef frame=CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
        
        
        
        CGContextRef context=UIGraphicsGetCurrentContext();
        CGContextSetTextMatrix(context, CGAffineTransformIdentity);
        CGContextSaveGState(context);
        CGContextTranslateCTM(context, 0, self.bounds.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);
        
        CTFrameDraw(frame, context);
        
        
        CGPathRelease(path);
        CFRelease(framesetter);
    }
    
    
    
}

- (void)setFont:(UIFont *)font{
    
    _font=font;
      [self setNeedsDisplay];
}

- (void)setText:(NSString *)text{
    _text=text;
       [self setNeedsDisplay];
}

- (void)setTextColor:(UIColor *)textColor{
    _textColor=textColor;
       [self setNeedsDisplay];
}


@end
